package com.vay.de.dang.online.phone;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

public class NetWorkHelper {

    final static String NETWORK_NONE = "none";
    final static String NETWORK_WIFI = "wifi";
    final static String NETWORK_2G = "2g";
    final static String NETWORK_3G = "3g";
    final static String NETWORK_4G = "4g";
    final static String NETWORK_5G = "5g";
    final static String NETWORK_MOBILE = "mobile";

    public static boolean isWiFiAvailable(Context context) {
        if (context == null) return false;
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wiFiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean connected = wiFiInfo.isConnected();
        if (connected) {
            return wiFiInfo.isAvailable();
        }
        return false;
    }

    @SuppressLint("MissingPermission")
    public static String getNetworkState(Context context) {
        //获取系统的网络服务
        ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connManager == null) return NETWORK_NONE;

        //如果当前没有网络
        //获取当前网络类型，如果为空，返回无网络
        NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
        if (activeNetInfo == null || !activeNetInfo.isAvailable()) {
            return NETWORK_NONE;
        }

        // 判断是不是连接的是不是wifi
        NetworkInfo wifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (null != wifiInfo) {
            NetworkInfo.State state = wifiInfo.getState();
            if (null != state) {
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    return NETWORK_WIFI;
                }
            }
        }

        // 如果不是wifi，则判断当前连接的是运营商的哪种网络2g、3g、4g等
        NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (null != networkInfo) {
            NetworkInfo.State state = networkInfo.getState();
            String strSubTypeName = networkInfo.getSubtypeName();
            int subtype = activeNetInfo.getSubtype();
            if (null != state) {
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    if (subtype == TelephonyManager.NETWORK_TYPE_GPRS
                            || subtype == TelephonyManager.NETWORK_TYPE_CDMA
                            || subtype == TelephonyManager.NETWORK_TYPE_EDGE
                            || subtype == TelephonyManager.NETWORK_TYPE_1xRTT
                            || subtype == TelephonyManager.NETWORK_TYPE_IDEN) {
                        return NETWORK_2G;
                    } else if (subtype == TelephonyManager.NETWORK_TYPE_EVDO_A
                            || subtype == TelephonyManager.NETWORK_TYPE_UMTS
                            || subtype == TelephonyManager.NETWORK_TYPE_EVDO_0
                            || subtype == TelephonyManager.NETWORK_TYPE_HSDPA
                            || subtype == TelephonyManager.NETWORK_TYPE_HSPA
                            || subtype == TelephonyManager.NETWORK_TYPE_HSUPA
                            || subtype == TelephonyManager.NETWORK_TYPE_EVDO_B
                            || subtype == TelephonyManager.NETWORK_TYPE_EHRPD
                            || subtype == TelephonyManager.NETWORK_TYPE_HSPAP) {
                        return NETWORK_3G;
                    } else if (subtype == TelephonyManager.NETWORK_TYPE_LTE) {
                        return NETWORK_4G;
                    } else if (subtype == TelephonyManager.NETWORK_TYPE_NR) {
                        return NETWORK_5G;
                    } else {
                        if (strSubTypeName.equalsIgnoreCase("TD-SCDMA") ||
                                strSubTypeName.equalsIgnoreCase("WCDMA") ||
                                strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                            return NETWORK_3G;
                        } else {
                            return NETWORK_MOBILE;
                        }
                    }
                }
            }
        }

        return NETWORK_NONE;
    }

    public static String getWifiMac(Context context) {
        String wifiMac = "";

        if (!isWiFiAvailable(context)) {
            return wifiMac;
        }

        try {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (wifiManager != null) {
                WifiInfo info = wifiManager.getConnectionInfo();
                if (info != null) {
                    wifiMac = info.getBSSID();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (wifiMac == null)
            wifiMac = "";

        return wifiMac;
    }

    /**
     * 获取wifi name
     */
    public static String getWifiName(Context context) {

        String wifiName = "";
        if (!isWiFiAvailable(context)) {
            return wifiName;
        }

        try {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (wifiManager != null) {
                WifiInfo info = wifiManager.getConnectionInfo();
                if (info != null) {
                    wifiName = info.getSSID();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (wifiName == null)
            wifiName = "";

        return wifiName;
    }
}
